﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using com.huigo.Framework.ServiceUtils;
using com.loto.Common;

namespace com.huigo.B2B.Web.center.order
{
    public partial class PolicyList : BasePage
    {
        /// <summary>
        /// 保单号
        /// </summary>
        private string queryOrderNo
        {
            get
            {
                return string.IsNullOrEmpty(Request.QueryString["policyNo"]) ? string.Empty : com.loto.Common.Utils.SqlEscape(Request.QueryString["policyNo"]);
            }
        }
        /// <summary>
        /// 开始时间
        /// </summary>
        private DateTime queryStart
        {
            get
            {
                return com.loto.Common.Utils.IsDate(Request.QueryString["Start"]) ? Convert.ToDateTime(Request.QueryString["Start"]) : DateTime.MinValue;
            }
        }
        /// <summary>
        /// 结束时间
        /// </summary>
        private DateTime queryEnd
        {
            get
            {
                return com.loto.Common.Utils.IsDate(Request.QueryString["End"]) ? Convert.ToDateTime(Request.QueryString["End"]) : DateTime.MinValue;
            }
        }
        /// <summary>
        /// 保单类型
        /// </summary>
        private long queryType
        {
            get
            {
                return string.IsNullOrEmpty(Request.QueryString["Type"]) ? long.MinValue : Convert.ToInt64(com.loto.Common.Utils.SqlEscape(Request.QueryString["Type"]));
            }
        }
        /// <summary>
        /// 被保人姓名
        /// </summary>
        private string policyInsuredName
        {
            get
            {
                return string.IsNullOrEmpty(Request.QueryString["insuredName"]) ? string.Empty : com.loto.Common.Utils.SqlEscape(Request.QueryString["insuredName"]);
            }
        }
        /// <summary>
        /// 证件号
        /// </summary>
        private string policyInsuredNo
        {
            get
            {
                return string.IsNullOrEmpty(Request.QueryString["insuredNo"]) ? string.Empty : com.loto.Common.Utils.SqlEscape(Request.QueryString["insuredNo"]);
            }
        }
        private int pageIndex
        {
            get
            {
                return com.loto.Common.TypeParse.StrToInt(Request.QueryString["page"], 1);
            }
        }
        public string Notiy = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            string url = "PolicyList.aspx?";
            url += "page=1";
            if (orderName.Text != string.Empty)
                url += "&policyNo=" + orderName.Text;
            if (beginDate.Text != string.Empty)
                url += "&Start=" + beginDate.Text;
            if (endDate.Text != string.Empty)
                url += "&End=" + endDate.Text;
            if (proType.SelectedValue != string.Empty)
                url += "&Type=" + proType.SelectedValue;
            if (insuredName.Text != string.Empty)
                url += "&insuredName=" + insuredName.Text;
            if (insuredNo.Text != string.Empty)
                url += "&insuredNo=" + insuredNo.Text;
            Response.Redirect(url);
        }

        protected void pageInfo_PageChanged(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
                if (proType.Items.Count > 0)
                {
                    for (int i = 0; i < proType.Items.Count; i++)
                    {
                        if (proType.Items[i].Value == queryType.ToString())
                        {
                            proType.SelectedIndex = i;
                        }
                    }
                }
                BindData();
            }
        }
        //保单类型数据绑定
        private void LoadData()
        {
            List<DropList> tDropList = Tools.DictToDropList(Dict.DictInsType);
            if (tDropList != null)
            {
                proType.DataSource = tDropList;
                proType.DataValueField = "Id";
                proType.DataTextField = "Value";
                proType.DataBind();
            };
        }
        //保单数据绑定
        private void BindData()
        {
            if (!string.IsNullOrEmpty(queryOrderNo))
            {
                orderName.Text = queryOrderNo;
            }
            if (!string.IsNullOrEmpty(policyInsuredName))
            {
                insuredName.Text = policyInsuredName;
            }
            if (!string.IsNullOrEmpty(policyInsuredNo))
            {
                insuredNo.Text = policyInsuredNo;
            }
            if (queryStart != DateTime.MinValue)
            {
                beginDate.Text = Convert.ToDateTime(queryStart).ToString("yyyy-MM-dd");
            }
            if (queryEnd != DateTime.MinValue)
            {
                endDate.Text = Convert.ToDateTime(queryEnd).ToString("yyyy-MM-dd");
            }
            List<RequestFuncParams> list = new List<RequestFuncParams>();
            list.Add(new RequestFuncParams("policyNo", queryOrderNo));
            list.Add(new RequestFuncParams("policyInsuredName", policyInsuredName));
            list.Add(new RequestFuncParams("policyInsuredNo", policyInsuredNo));
            list.Add(new RequestFuncParams("start", queryStart.ToString()));
            list.Add(new RequestFuncParams("end", queryEnd.ToString()));
            list.Add(new RequestFuncParams("type", queryType.ToString()));
            list.Add(new RequestFuncParams("custFid", CustId.ToString()));
            list.Add(new RequestFuncParams("pageSize", pageInfo.PageSize.ToString()));
            list.Add(new RequestFuncParams("pageIndex", pageIndex.ToString()));
            BaseService service = new BaseService();
            ParserXml parser = service.RequestEntryService(Dict.ERequestFunc.GET_ORDER_INFO_POLICY_LIST, list);
            if (parser != null && parser.RowsCount > 0)
            {
                pageInfo.RecordCount = Convert.ToInt16(parser["RecordCount", 0]);
                pageInfo.CurrentPageIndex = pageIndex;
                pageInfo.PageURLFormat = GetPagerUrl();
                PolicyListContent.DataSource = parser.ModelList;
                PolicyListContent.DataBind();
            }
            else
            {
                Notiy = "<table width=\"100%\" class=\"table table-bordered table-striped responsive-utilities table-hover\"><thead><tr><th>保单号</th><th>保险名称</th><th>被保人姓名</th><th>证件号码</th><th>保单金额</th><th>保单起期</th><th>保单状态</th><th>操作</th></tr></thead><tbody><tr><td colspan=\"8\" style=\"text-align:center;\">没有相关信息</td></tr></tbody></table>";
            }
        }
        private string GetPagerUrl()
        {
            string url = "PolicyList.aspx?";
            url += "page={0}";
            if (queryOrderNo != string.Empty)
                url += "&policyNo=" + queryOrderNo;
            if (queryStart != DateTime.MinValue)
                url += "&Start=" + queryStart;
            if (queryEnd != DateTime.MinValue)
                url += "&End=" + queryEnd;
            if (queryType > 0)
                url += "&Type=" + queryType;
            if (policyInsuredName != string.Empty)
                url += "&insuredName=" + policyInsuredName;
            if (policyInsuredNo != string.Empty)
                url += "&insuredNo=" + policyInsuredNo;
            return url;
        }

        public string ExitBtn(string status, string orderId, string policyId, DateTime time)
        {
            if (status == "2" && DateTime.Now <= time)
            {
                return "<a href=\"javascript:cancelPolicy('" + orderId + "','" + policyId + "','" + time + "');\">退保</a>";
            }
            else
            {
                return string.Empty;
            }
        }
    }
}
